package com.gopay.bis.manager.bank.Wap;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Map;

import javax.annotation.Resource;

import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.bis.common.util.BisPayCheckUtils;
import com.gopay.common.constants.bank.BankConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.psbc.ebank.PsbcB2cMessage;
import com.gopay.common.exception.GopayException;
import com.gopay.remote.bank.WapPayManagerRemoteService;
import com.gopay.remote.bank.businesses.BankService;

/**
 * 邮储银行wap支付manager
 * 
 * @PsbcWapB2CManager.java
 * @author zhengxue
 * @2016-12-1 上午11:15:45 www.gopay.com.cn Inc.All rights reserved.
 */
public class PsbcWapB2cManager implements WapPayManagerRemoteService {
    @Resource(name = "psbcB2cBankServiceClient")
    private BankService psbcB2cBankServiceClient;

    @Resource(name = "psbcConfig")
    private Map<String, String> psbcConfig;

    @Override
    public Object sign(BaseMessage message) throws GopayException {
        PsbcB2cMessage psbcB2cMessage = (PsbcB2cMessage) message;
        psbcB2cMessage.setBankUrl(psbcB2cMessage.getPayUrl()); // 请求银行URL
        psbcB2cMessage.setTranAbbr(psbcConfig.get("PSBC.B2C.WAP.TRANABBR"));
        psbcB2cMessage.setMercCode(psbcB2cMessage.getMerID());
        String merUrl = psbcB2cMessage.getPayRuturnUrl();
        try {
            psbcB2cMessage.setMercUrl(URLEncoder.encode(merUrl, "utf-8"));
        } catch (UnsupportedEncodingException e) {
            throw new GopayException("", e);
        }
        psbcB2cMessage.setRemark1(psbcConfig.get("PSBC.B2C.WAP.REMARK1"));
        psbcB2cMessage.setPlainMessage(buildPsbcB2c(psbcB2cMessage));
        BankLogUtils.printB2CManagerSignSendLog(psbcB2cMessage);
        String signStr = (String) psbcB2cBankServiceClient.sign(psbcB2cMessage);
        psbcB2cMessage.setSignMessage(signStr);
        BankLogUtils.printB2CManagerSignReturnLog(message);
        return psbcB2cMessage;
    }

    @Override
    public Object verify(BaseMessage message) throws GopayException {
        PsbcB2cMessage psbcB2cMessage = (PsbcB2cMessage) message;
        BankLogUtils.printB2CManagerVerifySendLog(psbcB2cMessage);
        boolean isVerify = (Boolean) psbcB2cBankServiceClient.verify(psbcB2cMessage);
        BankLogUtils.printB2CManagerVerifyReturnLog(psbcB2cMessage, isVerify);
        return isVerify;
    }

    @Override
    public Object query(Object obj) throws GopayException {
        PsbcB2cMessage psbcQueryMessage = (PsbcB2cMessage) obj;
        if (BisPayCheckUtils.checkExternalOrderID(psbcQueryMessage.getOrderId())) {
            psbcQueryMessage.setTermSsn(psbcQueryMessage.getOrderId());
        }
        psbcQueryMessage.setTranAbbr(psbcConfig.get("PSBC.B2C.QUERY.TRANSNAME"));
        psbcQueryMessage.setoTranAbbr(psbcConfig.get("PSBC.B2C.WAP.TRANABBR"));
        psbcQueryMessage.setMercCode(psbcQueryMessage.getMerID());
        psbcQueryMessage.setPlainMessage(buildQueryPlain(psbcQueryMessage));
        BankLogUtils.printB2CManagerQuerySendLog(psbcQueryMessage, psbcQueryMessage);
        PsbcB2cMessage result = (PsbcB2cMessage) psbcB2cBankServiceClient.query(psbcQueryMessage);
        result.setOutStlmCd(BankConstants.BANK_LOGO_PSBC);
        BankLogUtils.printB2CManagerQueryReturnLog(result);
        return result;
    }

    @Override
    public Object dayend(BaseMessage base) throws GopayException, Exception {
        // TODO Auto-generated method stub
        return null;
    }

    /**
     * 构建B2C签名明文
     * 
     * @param message
     * @return
     */
    private String buildPsbcB2c(PsbcB2cMessage message) {
        StringBuilder sb = new StringBuilder("");
        sb.append("TranAbbr=").append(message.getTranAbbr());
        sb.append("|MercDtTm=").append(message.getOrderDate());
        sb.append("|TermSsn=").append(message.getOrderId());
        sb.append("|OSttDate=").append(message.getoSttDate());
        sb.append("|OAcqSsn=").append(message.getoAcqSsn());
        sb.append("|MercCode=").append(message.getMercCode());
        sb.append("|TermCode=").append(message.getTermCode());
        sb.append("|TranAmt=").append(message.getAmount().toString());
        sb.append("|MercUrl=").append(message.getMercUrl());
        sb.append("|Remark1=").append(message.getRemark1());
        sb.append("|Remark2=").append(message.getRemark2());
        return sb.toString();
    }

    /**
     * 构建订单查询明文
     * 
     * @param message
     * @return
     */
    private String buildQueryPlain(PsbcB2cMessage message) {
        StringBuffer sf = new StringBuffer("");
        sf.append("OTranAbbr=").append(message.getoTranAbbr());
        sf.append("|MercCode=").append(message.getMercCode());
        sf.append("|TermSsn=").append(message.getTermSsn());
        return sf.toString();
    }

}
